package com.yq.cp.controller.permission;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yq.cp.config.UserThreadLocal;
import com.yq.cp.entity.common.ApiResult;
import com.yq.cp.entity.common.PageVo;
import com.yq.cp.entity.permission.Role;
import com.yq.cp.entity.permission.dto.RoleDto;
import com.yq.cp.entity.permission.vo.RoleVo;
import com.yq.cp.service.permission.RoleService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * <p>
 * 角色 前端控制器
 * </p>
 *
 * @author zh
 * @since 2022-09-21
 */
@RestController
@RequestMapping("/role")
public class RoleController {

    @Resource
    RoleService roleService;

    @PostMapping("add")
    public ApiResult add(@RequestBody RoleVo role){
        //最后执行结束需要将ThreadLocal中的信息删除  不删除会有内存泄露的风险
        role.setCreateUserId(UserThreadLocal.get().getId());
        roleService.add(role);
        return  ApiResult.ok();
    }
    @PostMapping("update")
    public ApiResult update(@RequestBody RoleVo role){
        roleService.update(role);
        return  ApiResult.ok();
    }
    @PostMapping("delete")
    public ApiResult delete(@RequestBody RoleVo role){
        roleService.delete(role);
        return  ApiResult.ok();
    }
    @PostMapping("page")
    public ApiResult page(@RequestBody PageVo vo){
        IPage<Role> page = roleService.page(vo);
        return  ApiResult.ok().data(page);
    }
    /** 查询角色并统计用户数量 */
    @PostMapping("pageAndUserNumber")
    public ApiResult pageAndUserNumber(@RequestBody RoleVo vo){
        IPage<RoleDto> page = roleService.pageAndUserNumber(vo);
        return  ApiResult.ok().data(page);
    }
    @PostMapping("get")
    public ApiResult get(@RequestBody Role role){
        Role byId = roleService.getById(role.getId());
        return  ApiResult.ok().data(byId);
    }
}

